<?php
	require_once('inc/fnc.php');
	require_once('inc/conn.php');
	require_once('inc/settings.php');
	session_start();
	//Retrieving title of the story
			
	$query = "SELECT * FROM story WHERE storyid = {$id} LIMIT 1";
 	$result = mysql_query($query, $connection) or die("Unable to retrieve Story Title " . mysql_error());
	if(mysql_num_rows($result) == 1) {
		while($row = mysql_fetch_array($result)){ $title = $row['storyname']; }
	}
	
	if(isset($_GET['nid'])) { readnotification($_GET['nid']); }

?>
<div id="content" class="">
    <div class="topShadow"></div>
    <div class="story">
    <!-- Story list -->
        <h1><?php echo ucfirst($title); ?></h1> 
        <ul id="updates" class="timeline"> 
            <?php
				$limit = LINEOFFSET + 1;		//Num of lines to be displayed at once (used in old framework)

				$query = "SELECT * FROM `storyline` WHERE `storyid` = {$id} AND `delflag` = 'N' ORDER BY storylineid ASC";
				$result = mysql_query($query, $connection) or die("1:005 " . mysql_error());
				$numRow = mysql_num_rows($result);
				$count = 1;
				while($row = mysql_fetch_array($result)) {
					$time = $row['crtdtime'];
					$serial = $row['storylineid'];
					echo getLine($id, $row['storylineid'], $row['storyline'], $row['crtdbyid']); //Display the lines
					$count = $count + 1;
				 }
				 
				 if($count > 100) {
					echo "<li class='eachLine'><h1>This story is completed</h1></li>";
				} else{
					echo getEntryForm($id, $serial);
				}
			 ?>
        </ul>
	
		<div class="social">
			<div class="each-social">
				<a href="https://twitter.com/share" class="twitter-share-button" data-via="nexttwist" data-related="nexttwist" data-hashtags="nexttwist">Tweet</a>
				<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
			</div>
			<div class="each-social">
				<div class="fb-like" data-href="<?php echo DOMAIN . $_SERVER['REQUEST_URI']; ?>" data-send="false" data-layout="button_count" data-width="95" data-show-faces="false" data-font="tahoma"></div>
			</div>
			<div class="each-social">
				<g:plusone size="medium" annotation="bubble" width="120"></g:plusone>
			</div>
			<div class="clear"></div>
		</div>
	
    <!-- Story lines ends --> 
    </div>
    <div class="bottomShadow"></div>
    
    <?php
		$lineId = getnewentryid($id);
		if($lineId <= 1){ $hide = 'hide';	}
	?>
    <div id="elPaneWrap" class="<?php echo $hide; ?>">
        <div id="twistTopS" class="topShadow"></div>
        <!-- Entry Lines starts -->
        <div id="twists" class="eLine" > 
            <h1>Vote the Next Twist</h1> 
            <ul id="ntwists" class="timeline" data-id="<?php echo $id; ?>"> 
                <?php
                    $slid = getnewstorylineid($id);
                    $query = "SELECT * FROM entryline WHERE `storyid` = {$id} and storylineid = {$slid} and delflag = 'N'";
                    $result = mysql_query($query,$connection) or die('error');
                    $i = 1;
                    
                    while($rows = mysql_fetch_array($result)) {                        
						$query1 = "SELECT count(vote) as voteup from entrylinevote WHERE storyid = '{$id}' and entrylineid = '{$rows['displayseq']}' and delflag = 'N' and vote = 1";
                        $result1 = mysql_query($query1,$connection);
						
                        while( $row1 = mysql_fetch_array($result1)) { $vtup = $row1['voteup']; }
                        
						$query2 = "SELECT count(vote) as votedwn from entrylinevote WHERE storyid = '{$id}' and entrylineid = '{$rows['displayseq']}' and delflag = 'N' and vote = -1";
                        $result2 = mysql_query($query2,$connection);
						
                        while( $row1 = mysql_fetch_array($result2)) { $vtdwn = $row1['votedwn']; }
						
						//If logged in, check whether the user has voted the line or not
						if(is_logged_in()){
							$query3 = "SELECT vote from entrylinevote WHERE storyid = '{$id}' and entrylineid = {$rows['displayseq']} and delflag = 'N' and votebyuserid = {$_SESSION['userId']}";
							$result3 = mysql_query($query3,$connection) or die (mysql_error());
							
							
							while($row1 = mysql_fetch_array($result3)) {
								$votetype = $row1['vote'];
								if($votetype == 1) { $upClass = "votedUp"; } elseif ($votetype == -1){ $dwnClass = "votedDwn"; }
							}
						} else {
							$upClass = "voteUp";
                        	$dwnClass = "voteDwn";
						}
                        $updateNode = "newline" . $i;
						$delNode = "line" . $i;
						$args = array (
							'count' => $i,
							'lineId' => $rows['entrylineid'],
							'line' => $rows['entryline'],
							'voteUp' => $vtup,
							'voteDwn' => $vtdwn,
							'updateNode' => $updateNode,
							'upClass' => $upClass,
							'dwnClass' => $dwnClass,
							'delNode' => $delNode,
							'deleter' => $_SESSION['deleter'],
							'user' => $rows['crtdbyid'],
							'level' => 1,
							'uniqueid' => $rows['displayseq'],
							'crtdbyid' => $rows['crtdbyid'],
							'stryid' => $slid,
						);
                                
						$entry .= getEntryLine($args);
						$i++;
                   } // while ($i < $count) ENDS
                    echo $entry;
                    // If the file does not exist
                ?>
            </ul> 
        </div>  
        <!-- Entry Line ends -->
        <div id="twistBottomS" class="bottomShadow"></div>
    </div>
</div>